package com.mobicartel.androidto;

import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Looper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TableLayout;
import android.widget.TextView;

public class SpeakerActivity extends Activity implements OnClickListener {
	
	private List<Speaker> list_speakers;
	
	private TableLayout speaker_holder;
	LinearLayout.LayoutParams params_speaker_holder;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.speakers);
	
		speaker_holder = (TableLayout) findViewById(R.id.SpeakerRowLayout);
		
		new DownloadSpeakers().execute("http://androidguys.savina.net/speakers_xml.xml");
		
	}
	
	@Override
	public void onStart() {
		super.onStart();
		
		
	}
	
	private class DownloadSpeakers extends AsyncTask<String, Integer, List<Speaker>> {

		@Override
		protected List<Speaker> doInBackground(String... url) {
			
			Looper.prepare();
			
			XmlPullFeedParser parser = new XmlPullFeedParser();
	        list_speakers = parser.parse(url[0]);
			
			return list_speakers;
		}
		
		@Override
	    protected void onPostExecute(List<Speaker> result) {
			
			DisplaySpeakers(result);
			
	    }

	}
	
	private void DisplaySpeakers(List<Speaker> result) {
		
		LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

		int int_bullet_switcher = 0;
		for (int x = 0; x < result.size(); x++) {
			
			View speakerView = inflater.inflate(R.layout.speaker_row, speaker_holder);
			
			TextView name = (TextView) speakerView.findViewById(R.id.txt_name);
			ImageView bullet = (ImageView) speakerView.findViewById(R.id.img_bullet);
			ImageView pic = (ImageView) speakerView.findViewById(R.id.img_pic);
			Button btn_info = (Button) speakerView.findViewById(R.id.btn_info);
			
			pic.setImageResource(R.drawable.cs);
			name.setText(result.get(x).getName().toString());
			switch(int_bullet_switcher) {
			case(0):
				bullet.setImageResource(R.drawable.img_black_tile);
				int_bullet_switcher++;
				break;
			case(1):
				bullet.setImageResource(R.drawable.img_green_tile);
				int_bullet_switcher--;
				break;
			}
			
			
			
		}
		
		
	}
	
	@Override
	public void onClick(View view) {
		
		Bundle speaker_bundle = new Bundle();
		
	}
		
}

